package com.lpj.call.controller;

import com.lpj.call.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String index(Model model){
        model.addAttribute("state",userService.list());
        return "index";
    }

    @GetMapping("/sign")
    public String sign(String name, Model model){
        if (! userService.update(name)){
            return "403";
        }
        model.addAttribute("state",userService.list());
        return "index";
    }

    @PostMapping("/load")
    public String load(MultipartFile file, Model model){
        String fileName = file.getOriginalFilename();
        try {
            userService.Import(fileName,file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("state",userService.list());
        return "index";
    }

}
